var request = require('../../utils/request.js');
var fid = ''
Page({
    data: {
        type: '',
        title: '',
        text: '',
        imgList: [],
        pidList: [],
        qq: '',
        wechat: '',
        tel: '',
        mail: '',
        notice: '',
        noticeShow: false,
        hide: true,
        openid:''
    },

    onLoad() {
        var that=this
        this.getFid()
        this.getNotice()
        // qq.cloud.database().collection("hide").get().then(res => {
        //     if (!res.data[0].hide) {
        //         this.setData({
        //             hide: false
        //         })
        //     }
        // })
        qq.cloud.callFunction({
            name:'login',
            success(res){
                console.log(res)
                that.setData({
                    openid:res.result.OPENID
                })
            },            
        })
    },

    manage() {
        console.log("asfdsafa")
        qq.switchTab({
            url: "/pages/new/new",
            fail(err) {
                console.log(err)
            }
        })
    },

    getFid() {
        let that = this
        request.GET('/api/form/').then(
            function (res) {
                fid = res.data.fid
                that.setData({
                    type: '',
                    title: '',
                    text: '',
                    imgList: [],
                    pidList: [],
                    qq: '',
                    wechat: '',
                    tel: '',
                    mail: ''
                })
            }
        )
    },

    submit() {
        if (this.data.type == '' || this.data.title == '') {
            qq.showToast({
                title: '请补全类型和标题',
                icon: 'none'
            })
            return
        }
        // 测试区域
        qq.navigateTo({
            url: '../preview/preview'
        })
        return
        // console.log(this.toJSON(this.data.qq,this.data.wechat,this.data.tel,this.data.mail))
        if (this.data.type == '' || this.data.title == '') {
            qq.showToast({
                title: '请补全类型和标题',
                icon: 'none'
            })
            return
        }

        console.log('预览')
        var that = this
        var contact = {
            qq: this.data.qq,
            wechat: this.data.wechat,
            tel: this.data.tel,
            mail: this.data.mail
        }
        console.log(fid)
        var params = {
            fid: parseInt(fid),
            type: this.data.type,
            title: this.data.title,
            content: this.data.text,
            contact: JSON.stringify(contact)
        }
        request.POST('/api/form/publish/', params).then(res => {
            console.log(res)
            if (res.statusCode == 201) {
                that.getFid()
                qq.navigateTo({
                    url: '../wait/wait'
                })
            } else {
                qq.showToast({
                    title: '提交失败',
                    icon: 'none'
                })
            }
        })
    },
    // 预览后调用的提交
    realSubmit() {
        qq.showLoading({
            title: '提交中',
            mask: true
        })
        var that = this
        var contact = {
            qq: this.data.qq,
            wechat: this.data.wechat,
            tel: this.data.tel,
            mail: this.data.mail
        }
        console.log(fid)
        var params = {
            fid: parseInt(fid),
            type: this.data.type,
            title: this.data.title,
            content: this.data.text,
            contact: JSON.stringify(contact),
            openid:this.data.openid
        }
        request.POST('/api/form/publish/', params).then(res => {
            console.log(res)
            qq.hideLoading()
            if (res.statusCode == 201) {
                that.getFid()
                qq.redirectTo({
                    url: '../wait/wait'
                })
            } else {
                qq.showToast({
                    title: '提交失败',
                    icon: 'none'
                })
            }
        }, res => {
            console.log(res)
            qq.showToast({
                title: '请求错误',
                icon: 'none'
            })
        })
    },
    ChooseImage() {
        qq.chooseImage({
            count: 1, //选择1张图片
            success: (res) => {
                console.log(res)
                this.setData({
                    imgList: this.data.imgList.concat(res.tempFilePaths)
                })
                var tempFilePaths = res.tempFilePaths
                var params = { fid: fid }
                request.UPLOAD('/api/form/pic/', 'pic', tempFilePaths[0], params).then(res => {
                    console.log(res)
                    var data = JSON.parse(res.data)
                    this.setData({
                        pidList: this.data.pidList.concat(data.pid)
                    })
                })
            }
        });
    },

    ViewImage(e) {
        qq.previewImage({
            urls: this.data.imgList,
            current: e.currentTarget.dataset.url
        });
    },

    DelImg(e) {
        var pid = this.data.pidList[e.currentTarget.dataset.index]
        var params = { pid: pid }
        request.DELETE('/api/form/pic/' + pid + '/', params).then(res => {
            console.log(res)
        })
        this.data.pidList.splice(e.currentTarget.dataset.index, 1);
        this.data.imgList.splice(e.currentTarget.dataset.index, 1);
        this.setData({
            imgList: this.data.imgList
        })
    },

    typeInput(e) {
        this.setData({
            type: e.detail.value == undefined ? '' : e.detail.value
        })
    },
    titleInput(e) {
        this.setData({
            title: e.detail.value == undefined ? '' : e.detail.value
        })
    },
    textInput(e) {
        this.setData({
            text: e.detail.value == undefined ? '' : e.detail.value
        })
    },
    qqInput(e) {
        this.setData({
            qq: e.detail.value == undefined ? '' : e.detail.value
        })
    },
    wechatInput(e) {
        this.setData({
            wechat: e.detail.value == undefined ? '' : e.detail.value
        })
    },
    telInput(e) {
        this.setData({
            tel: e.detail.value == undefined ? '' : e.detail.value
        })
    },
    mailInput(e) {
        this.setData({
            mail: e.detail.value == undefined ? '' : e.detail.value
        })
    },

    onShareAppMessage: function () {
        qq.showShareMenu({
            showShareItems: ['qq', 'qzone', 'wechatFriends', 'wechatMoment']
        })
    },

    // 获取通知
    getNotice() {
        let that = this
        request.GET('/api/broadcastlatest/').then(res => {
            console.log(res)
            if (!res.data.value) {
                this.setData({
                    noticeShow: false
                })
            } else {
                that.setData({
                    notice: res.data.value,
                    noticeShow: true
                })
            }
        })
    },
    // 关闭通知
    noticeClose() {
        this.setData({
            noticeShow: false
        })
    }

})